package cn.com.sise.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.com.sise.dao.IBookDao;
import cn.com.sise.entity.Book;
import cn.com.sise.utils.DBConn;

public class BookDaoImpl implements IBookDao {
	
	private Connection conn = null;
	private PreparedStatement ps;

	// 图书添加
	public int bookInsert(Book book) throws Exception {
		String sql = "insert into tb_book(id,name,author,price,press,ISBN,pic) values(?,?,?,?,?,?,?)";
		conn=DBConn.getConnection(); 
		ps=conn.prepareStatement(sql);
		ps.setInt(1, book.getId());
		ps.setString(2, book.getName());
		ps.setString(3, book.getAuthor());
		ps.setFloat(4, book.getPrice());
		ps.setString(5, book.getPress());
		ps.setString(6, book.getISBN());
		ps.setString(7, book.getPic());
		// 执行SQL语句
		int flag = ps.executeUpdate();
		return flag;
	}

	// 查询所有图书
	public List<Book> queryAll() throws Exception {
		String sql = "select * from tb_book";		
		List<Book> books=new ArrayList<Book>();
		conn=DBConn.getConnection(); 
		ps=conn.prepareStatement(sql);
		ResultSet rs=ps.executeQuery();
		while(rs.next()) {
			Book book=new Book();
			book.setId(rs.getInt("id"));
			book.setName(rs.getString("name"));
			book.setAuthor(rs.getString("author"));
			book.setPrice(rs.getFloat("price"));
			book.setPress(rs.getString("press"));
			book.setISBN(rs.getString("ISBN"));
			book.setPic(rs.getString("pic"));
			books.add(book);
		}
		return books;
	}



}
